package com.bdqn.demo.util;

import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory=null;
    static {
        //第一步,将mybatis-config.xml转成inputStream流
        InputStream inputStream= ResolverUtil.Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        //第二步,创建SqlSessionFactory对象,通过new SqlSessionFactoryBuild的build方法创建
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    }
    //定义获取SqlSession方法
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }

    //定义关闭SqlSession的方法
    public static void closeSqlSession(SqlSession sqlSession){
        sqlSession.close();
    }
}
